package site.lisl.mynote.algorithm.queue;

/**
 * @author Administrator
 */
public class ArrayQueue {

    private Object[] items;
    private Integer head;
    private Integer tail;

    private Integer size;


    public ArrayQueue(int capacity) {
        this.items = new Object[capacity];
        this.head = 0;
        this.tail = 0;
        this.size = capacity;
    }

    /**
     * 入队
     * @param obj
     * @return
     */
    public boolean enQueue(Object obj) {
        if(tail.equals( size )){
            if(head.equals(0)){
                //队列已满
                return false;
            }

            //数据整理
            for(int i = head; i < tail; i++){
                items[i-head] = items[i];
            }
            tail = tail - head;
            head = 0;
        }
        items[tail++] = obj;
        return true;
    }

    public Object deQueue(){
        if(head.equals(tail)){
            //表示空队列
            return null;
        }
        return items[head++];
    }
}
